package com.zhidian.edm.controller;

import com.zhidian.edm.param.AddMenuParam;
import com.zhidian.edm.param.UpdateMenuParam;
import com.zhidian.edm.result.ResultResponse;
import com.zhidian.edm.service.ISysMenuService;
import com.zhidian.edm.vo.RouterVO;
import com.zhidian.edm.vo.SysMenuDetailVO;
import com.zhidian.edm.vo.SysMenuVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sl
 * @since 2025-09-18
 */
@RestController
@RequestMapping("/menu")
@Tag(name = "获取菜单信息", description = "获取菜单信息")
public class SysMenuController {

    @Resource
    private ISysMenuService sysMenuService;

    @GetMapping("/router")
    @Operation(summary = "根据角色获取路由信息", description = "根据角色获取路由信息")
    public ResultResponse<List<RouterVO>> getMenuRouters(HttpServletRequest req){
        return ResultResponse.success(sysMenuService.getMenuRouters(req));
    }


    @GetMapping("/all")
    @Operation(summary = "获取所有菜单树无权限", description = "获取所有菜单树无权限")
    public ResultResponse<List<SysMenuVO>> getAllMenuTree(){
        return ResultResponse.success(sysMenuService.getAllMenuTree());
    }

    @GetMapping("/visible")
    @Operation(summary = "获取所有可见菜单树无权限", description = "获取所有可见菜单树无权限")
    public ResultResponse<List<SysMenuVO>> getAllMenuTreeAndNoVisible(){
        return ResultResponse.success(sysMenuService.getAllMenuTreeAndNoVisible());
    }

    @GetMapping("/role")
    @Operation(summary = "根据角色获取菜单树", description = "根据角色获取菜单树")
    public ResultResponse<List<SysMenuVO>> getAllMenuTree(@RequestParam(value = "roleIds", required = true) List<Long> roleIds){
        return ResultResponse.success(sysMenuService.getCurrentUserMenuTree(roleIds));
    }

    @GetMapping("/{menuId}")
    @Operation(summary = "根据menuId获取详情", description = "根据menuId获取详情")
    public ResultResponse<SysMenuDetailVO> getMenuInfoById(@PathVariable("menuId") Long menuId){
        return ResultResponse.success(sysMenuService.getMenuInfoById(menuId));
    }

    @DeleteMapping("/{menuId}")
    @Operation(summary = "根据menuId删除菜单", description = "根据menuId删除菜单")
    public ResultResponse deleteMenuById(@PathVariable("menuId") Long menuId){
        sysMenuService.deleteMenuById(menuId);
        return ResultResponse.success();
    }


    @PostMapping("/add")
    @Operation(summary = "添加菜单", description = "添加菜单")
    public ResultResponse addMenu(@RequestBody AddMenuParam addMenuParam){
        sysMenuService.addMenu(addMenuParam);
        return ResultResponse.success();
    }

    @PutMapping("/update")
    @Operation(summary = "更新菜单", description = "更新菜单")
    public ResultResponse updateMenu(@RequestBody UpdateMenuParam updateMenuParam){
        sysMenuService.updateMenu(updateMenuParam);
        return ResultResponse.success();
    }
}
